<template>
	<view class="container">
		<view class="status-bar"></view>
		<view class="body">
			<view class="input-view" style="margin-bottom: 10px;">
				<input placeholder="我需要..." type="text" v-model="title" />
			</view>
			<view class="content-view">
				<textarea v-model="content" maxlength="200" placeholder="详细的描述" class="content" />
				<text class="text-length">
					<text class="max-len" v-if="content.length === 200">
						200
					</text>
					<text v-else>
						{{content.length}}
					</text>
					/200
				</text>
			</view>
			<view class="images">
				<view class="item" v-for="(item, index) in uploadImages" :key="index">
					<image :src="item" class="image"></image>
					<uni-icons @tap="deleteImage(index)" type="trash" size="18" color="red" class="delete">
					</uni-icons>
				</view>
				<view class="item upload-btn flex-row" @click="chooseImage" v-if="uploadImages.length<3">
					<uni-icons type="plusempty" size="30" color="#ccc" class="add"></uni-icons>
				</view>
			</view>
			<!-- 下面的输入框组 -->
			<view class="items">
				<view class="tag-box">
					<view class='tag-item tag-item-orange' v-for="(tag,i) in tags" :key="i">
						<image src="https://cacode-ssb.oss-cn-hangzhou.aliyuncs.com/static/img/home/delete.png"
							mode="aspectFit" class="close" @tap="deleteTag(i)">
						</image>
						{{tag}}
					</view>
				</view>
				<view class="item flex-row">
					<view class="input-view" style="margin-bottom: 10px;">
						<input placeholder="回车新键标签" type="text" v-model="tempTag" @confirm="createTag($event)" />
					</view>
				</view>
				<view class="item flex-row">
					<view class="flex-row">
						<text class="label">送到哪里</text>
					</view>
					<view class="flex-row" @click="goMap">
						<input placeholder="点击选择地址" disabled="true" v-model="locationAddress"
							style="font-size: 20rpx;" />
					</view>
				</view>
				<view class="item flex-row">
					<view class="flex-row">
						<text class="label">选择分类</text>
					</view>
					<view class="flex-row" @tap="showModule">
						<text class="value" v-if="classify.name">{{classify.name}}</text>
						<text class="value" v-else>点击选择</text>
						<uni-icons type="arrowright" color="#999" size="18"></uni-icons>
					</view>
				</view>
				<view class="item flex-row">
					<view class="flex-row">
						<text class="label">帮忙价格</text>
					</view>
					<view class="flex-row">
						<input placeholder="开个价(元)" v-model="money" type="number" style="font-size: 20rpx;" />
					</view>
				</view>
				<view class="item flex-row">
					<view class="flex-row">
						<text class="label">联系方式</text>
					</view>
					<view class="flex-row">
						<input placeholder="手机号" v-model="phone" type="number" style="font-size: 20rpx;" />
					</view>
				</view>
			</view>
			<view class="protocol flex-row">
				<view class="text">如果填写，请你知悉并同意：你的联系方式会展现给有兴趣的同学</view>
			</view>
			<!--选择模块-->
			<jpSelect ref="jpSelect" :list="classifyList" @checked="checked" :item="classify" select="radio"
				height='30vh' tite="请选择分类">
			</jpSelect>
			<button type="warn" @click="send">发布</button>
		</view>
	</view>
</template>

<script>
	import jpSelect from '@/components/jp-select/jp-select.vue';
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import {
		APIHOST1
	} from '@/common/config.js'
	export default {
		components: {
			uniPopup,
			jpSelect
		},
		data() {
			return {
				// 临时的tag输入框
				tempTag: [],
				// 标签
				tags: [],
				// 详细描述
				content: '',
				// 标题
				title: '',
				// 设置的金额
				money: '',
				// 手机号码
				phone: '',
				// 位置
				locationAddress: '',
				ipAddress: '',
				// 上传的图片列表
				uploadImages: [],
				// 分类
				classify: {
					id: '',
					name: ''
				},
				classifyList: []
			}
		},
		onLoad(options) {
			this.$Request.getT('/fontend/classifys/').then(res => {
				if (res.code === this.$httpStatus.HTTP_200_OK) {
					this.classifyList = res.data
				}
			});
			if (options.hasOwnProperty('title')) {
				this.title = options['title']
			}
			if (options.hasOwnProperty('phone')) {
				this.phone = options['phone']
			}
		},
		methods: {
			deleteTag(index) {
				this.tags.splice(index, 1)
			},
			createTag(event) {
				if (this.tags.length < 3) {
					this.tags.push(event.detail.value)
					this.tempTag = ''
				} else {
					uni.showModal({
						showCancel: false,
						content: '最多输入三个标签'
					})
				}
			},
			// 点击调起地图选择位置
			goMap() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.locationAddress = res.address
						that.ipAddress = res.longitude + ',' + res.latitude
					}
				});
			},

			/**
			 * 删除图片
			 * @param {Object} index 删除的图片索引
			 */
			deleteImage(index) {
				this.uploadImages.splice(index, 1)
			},
			checked(el) {
				console.log(el)
				this.classify = el
			},
			showModule() {
				this.$refs.jpSelect.toOpen()
				// this.$refs.module.open()
			},
			/**
			 * 选择图片
			 */
			chooseImage() {
				uni.chooseImage({
					count: 3,
					sizeType: "original",
					success: (res) => {
						for (var i = 0; i < res.tempFilePaths.length; i++) {
							this.uploadImages.push(res.tempFilePaths[i])
						}
					}
				})
			},
			send() {
				if (!this.title) {
					uni.showModal({
						content: '请先填写标题',
						title: '信息未填写完整',
						showCancel: false
					})
					return
				}
				if (!this.content) {
					uni.showModal({
						content: '请先填写描述',
						title: '信息未填写完整',
						showCancel: false
					})
					return
				}
				if (!this.money) {
					uni.showModal({
						content: '请先设置金额',
						title: '信息未填写完整',
						showCancel: false
					})
					return
				}
				if (!this.phone) {
					uni.showModal({
						content: '请先填写联系手机号',
						title: '信息未填写完整',
						showCancel: false
					})
					return
				}
				if (!this.locationAddress) {
					uni.showModal({
						content: '请先填写地址',
						title: '信息未填写完整',
						showCancel: false
					})
					return
				}
				uni.showLoading({
					title: '正在发布...'
				})
				this.$Request.postT('/need/release/', {
					content: this.content,
					title: this.title,
					// 设置的金额
					money: this.money,
					// 手机号码
					phone: this.phone,
					// 位置
					address: this.locationAddress,
					classify_id: this.classify.id,
					location: this.ipAddress,
					tags: this.tags
				}).then(res => {
					uni.hideLoading()
					uni.showLoading({
						title: '正在上传图片.'
					})
					for (var i = 0; i < this.uploadImages.length; i++) {
						uni.uploadFile({
							url: APIHOST1 + '/need/upload/', //post请求的地址
							filePath: this.uploadImages[i],
							name: 'img',
							formData: {
								needId: res.data.id
							},
							header: {
								"Authorization": uni.getStorageSync("token")
							},
							success: (uploadFileRes) => {
								//这里要注意，uploadFileRes.data是个String类型，要转对象的话需要JSON.parse一下
								var obj = JSON.parse(uploadFileRes.data);
							}
						})
					}
					uni.hideLoading()
					uni.showToast({
						title: '发布成功，请等待审核',
						icon: 'success',
						duration: 3000
					});
					uni.navigateTo({
						url: '/pages/order/index'
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	view,
	text {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	.status-bar {
		height: var(--status-bar-height);
	}

	.flex-row {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.container {
		height: 100vh;
		overflow: hidden;
	}

	.module {
		width: 80vw;
		height: 600rpx;
		padding: 30rpx;
		overflow-y: auto;
		margin: 25vh auto 0;
		background-color: #FFF;

		.item {
			height: 80rpx;
			justify-content: flex-start;

			.text {
				margin-left: 20rpx;
			}
		}
	}

	.body {
		padding: 30rpx;
		overflow-y: auto;
		height: calc(100vh - 88rpx - var(--status-bar-height));

		.protocol {
			align-items: flex-start;

			.text {
				color: #999;
				font-size: 28rpx;
				width: calc(100% - 60rpx);
			}
		}

		.items {
			padding: 20rpx 0;

			.tag-box {
				display: flex;
				margin-bottom: 20upx;

				.tag-item {
					display: inline-block;
					height: 38upx;
					line-height: 38upx;
					border-radius: 5upx;
					padding: 0 40upx 0 10upx;
					margin-right: 10upx;
					position: relative;

					.close {
						width: 15px;
						height: 15px;
						position: absolute;
						right: 0;
						top: 0;
					}
				}

				.tag-item-blue {
					background: #D9EDFF;
					color: #66A6FF;
				}

				.tag-item-orange {
					background: #FFE8D9;
					color: #FF7D26;
				}

				.tag-item-pink {
					background: #FFD9D9;
					color: #FF6666;
				}

				.tag-item-green {
					background: #D9FFFB;
					color: #17D2BD;
				}
			}

			.item {
				height: 100rpx;
				border-bottom: 2rpx solid #F2F2F2;

				.label {
					color: #666;
					font-size: 30rpx;
					margin-left: 20rpx;
				}

				.value {
					color: #999;
					font-size: 28rpx;
				}
			}
		}

		.input-view {
			padding: 20rpx 30rpx;
			border-radius: 10rpx;
			width: 100%;
			background-color: white;

			input {
				font-size: 28rpx;
			}
		}

		.content-view {
			position: relative;

			.text-length {
				color: gray;
				font-size: 16rpx;
				position: absolute;
				right: 0;
				top: 0px;

				.max-len {
					color: red;
				}
			}

			.content {
				height: 200rpx;
				font-size: 17rpx;
				padding: 20rpx 30rpx;
				border-radius: 10rpx;
				width: 100%;
				background-color: white;
			}
		}

		.images {
			overflow: hidden;

			.item {

				float: left;
				margin: 20rpx 20rpx 0 0;
				width: calc((100vw - 100rpx) / 3);
				height: calc((100vw - 100rpx) / 3);
				position: relative;

				&:nth-child(3n) {
					margin-right: 0;
				}

				&.upload-btn {
					cursor: pointer;
					flex-direction: column;
					justify-content: center;
					border: 2rpx solid #ededed;
				}

				.image {
					width: 100%;
					height: 100%;
				}

				.delete {
					position: absolute;
					top: 0;
					right: 0;
					z-index: 9;
				}
			}
		}
	}

	.header {
		height: 88rpx;
		padding: 0 30rpx 0 20rpx;
		border-bottom: 2rpx solid #F2F2F2;

		.left,
		.right {
			width: 100rpx;
		}

		.center {
			text-align: center;
			width: calc(100% - 200rpx);
		}

		.right {
			text-align: right;
		}
	}
</style>
